﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Editor;
using ESRI.ArcGIS.EditorExt;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.GlobeCore;
using ESRI.ArcGIS.NetworkAnalysis;
using ESRI.ArcGIS.Output;
using ESRI.ArcGIS.SystemUI;
using System.Windows.Forms;

using System.Collections;
using DIWAGIS.Modules;

namespace DIWAGIS.Module
{
internal class WorkingWithDomain
    {

    public void LoadSubTypeToCombobox(ComboBox cb, string strTableName, string strFileName, IWorkspace pWS)
        {

        ArrayList arr = new ArrayList();
        IFeatureClass pFeatcls;
        ISubtypes pSubtypes;
        //IFeatureLayer pFeatLayer;
        IFeatureWorkspace gFeatureWS = pWS as IFeatureWorkspace;
        pFeatcls = gFeatureWS.OpenFeatureClass(strTableName);
        pSubtypes = (ISubtypes) pFeatcls;

        if ((pSubtypes.SubtypeFieldName == strFileName))
            {
                IEnumSubtype pEnumSubTypes;


                //IEnumSubtype enumSubtype;
                //int subtypeCode;
                //string subtypeName;
                //if (subtypes.HasSubtype)
                //{
                //    enumSubtype = subtypes.Subtypes;
                //    subtypeName = enumSubtype.Next(out subtypeCode);
                //    while (subtypeName != null)
                //    {
                //        Console.WriteLine("{0} has a subtype code of {1} and a name of {2}", nameOfFeatureClass, subtypeCode, subtypeName);
                //        subtypeName = enumSubtype.Next(out subtypeCode);
                //    }

                //}

                int lSubT;
                string sSubT;
                pEnumSubTypes = pSubtypes.Subtypes;
                sSubT = pEnumSubTypes.Next(out lSubT);
                while (sSubT != null)
                    {
                        cbb_Item cbb = new cbb_Item();
                        cbb.MText = sSubT;
                        cbb.MValue = lSubT.ToString();
                        arr.Add(cbb);
                        sSubT = pEnumSubTypes.Next(out lSubT);

                    }

                cb.DataSource = arr;
                cb.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                cb.AutoCompleteSource = AutoCompleteSource.ListItems;
                cb.DisplayMember = "mText";
                cb.ValueMember = "mValue";
            }
        return;

        }

    public void loadDataDomainToCombobox(ComboBox cbb, IWorkspace pWS, string pNameDomain)
        {

        IWorkspaceDomains pWSDomains;
        IDomain pDomain;
        pWSDomains = (IWorkspaceDomains) pWS;
        pDomain = (IDomain) pWSDomains.get_DomainByName(pNameDomain);

        DataTable tblResult = new DataTable();
        DataColumn colCode = new DataColumn("Code", typeof (string));
        DataColumn colValue = new DataColumn("Value", typeof (string));
        tblResult.Columns.Add(colCode);
        tblResult.Columns.Add(colValue);

        if (pDomain.Type == esriDomainType.esriDTCodedValue)
            {
                ICodedValueDomain CodeDomain = pDomain as ICodedValueDomain;
                for (int i = 0; i < CodeDomain.CodeCount; i++)
                    {
                        DataRow newRow = tblResult.NewRow();
                        newRow[0] = CodeDomain.get_Value(i).ToString();
                        newRow[1] = CodeDomain.get_Name(i);
                        tblResult.Rows.Add(newRow);
                    }
            }
        DataRow FistRow = tblResult.NewRow();
        //FistRow[1] = "Không chọn";
        tblResult.Rows.InsertAt(FistRow, 0);


        try
            {

                cbb.DataSource = tblResult;
                cbb.DropDownStyle = ComboBoxStyle.DropDownList;
                cbb.DisplayMember = "Value";
                cbb.ValueMember = "Code";
                cbb.AutoCompleteMode = AutoCompleteMode.Suggest;
                cbb.AutoCompleteSource = AutoCompleteSource.ListItems;
                cbb.SelectedIndex = -1;


            }
        catch (Exception ex)
            {
                //generalVariable.writeLogFileError("general_Model", "loadDataDomainToCombobox", ex.Message);

            }
        //loadDataToCombobox
        }

    public class cbb_Item
        {
        private string mText;
        private string mValue;

        public string MText
            {
            get { return mText; }
            set { mText = value; }
            }

        public string MValue
            {
            get { return mValue; }
            set { mValue = value; }
            }

        }

    public Boolean loadDataToCombobox(ComboBox cbb, SqlCommand command)
        {
        ArrayList array;
        DataTable dt;
        cbb_Item item;

        dt = executeSQLQueryReturnDataTable(command);

        try
            {
                if (dt != null && dt.Rows.Count > 0)
                    {

                        //array = new ArrayList();
                        DataTable tblResult = new DataTable();
                        DataColumn colCode = new DataColumn("Code", typeof (string));
                        DataColumn colValue = new DataColumn("Value", typeof (string));
                        tblResult.Columns.Add(colCode);
                        tblResult.Columns.Add(colValue);


                        for (int i = 0; i < dt.Rows.Count; i++)
                            {
                                //item = new cbb_Item();
                                //item.MText = dt.Rows[i][0].ToString();
                                //item.MValue = dt.Rows[i][1].ToString();
                                //array.Add(item);
                                DataRow newRow = tblResult.NewRow();
                                newRow[0] = dt.Rows[i][0].ToString();
                                newRow[1] = dt.Rows[i][1].ToString();
                                tblResult.Rows.Add(newRow);
                            }
                        DataRow FistRow = tblResult.NewRow();
                        //FistRow[1] = "Không chọn";
                        tblResult.Rows.InsertAt(FistRow, 0);
                        //cbb.DataSource = array;
                        cbb.DataSource = tblResult;
                        cbb.DisplayMember = "Code";
                        cbb.ValueMember = "Value";
                        cbb.AutoCompleteMode = AutoCompleteMode.Suggest;
                        cbb.AutoCompleteSource = AutoCompleteSource.ListItems;
                        cbb.SelectedIndex = -1;

                    }
                return true;
            }
        catch (Exception ex)
            {
                //generalVariable.writeLogFileError("general_Model", "loadDataToCombobox", ex.Message);
                return false;
            }

        }


    public static string GetNameFromCode(string code, IDomain domain)
        {
        string Name = string.Empty;
        if (domain.Type == esriDomainType.esriDTCodedValue)
            {
                ICodedValueDomain CodeDomain = domain as ICodedValueDomain;
                for (int i = 0; i < CodeDomain.CodeCount; i++)
                    {
                        string pCode = CodeDomain.get_Value(i).ToString();
                        if (pCode == code)
                            {
                                Name = CodeDomain.get_Name(i);
                                break;
                            }
                    }
            }
        return Name;
        }

    public static string GetNameFromCode(string code, IWorkspace pWS, string pNameDomain)
        {
        IWorkspaceDomains pWSDomains;
        IDomain pDomain;
        pWSDomains = (IWorkspaceDomains) pWS;
        pDomain = (IDomain) pWSDomains.get_DomainByName(pNameDomain);
        string Name = string.Empty;
        if (pDomain.Type == esriDomainType.esriDTCodedValue)
            {
                ICodedValueDomain CodeDomain = pDomain as ICodedValueDomain;
                for (int i = 0; i < CodeDomain.CodeCount; i++)
                    {
                        string pCode = CodeDomain.get_Value(i).ToString();
                        if (pCode == code)
                            {
                                Name = CodeDomain.get_Name(i);
                                break;
                            }
                    }
            }
        return Name;
        }



    /// <summary>
    /// Chuyển domain thành bảng
    /// </summary>
    /// <param name="domain"></param>
    /// <returns></returns>
    public static DataTable ConvertDomainToTable(IDomain domain)
        {
        DataTable tblResult = new DataTable();
        DataColumn colCode = new DataColumn("Code", typeof (string));
        DataColumn colValue = new DataColumn("Value", typeof (string));
        tblResult.Columns.Add(colCode);
        tblResult.Columns.Add(colValue);

        if (domain.Type == esriDomainType.esriDTCodedValue)
            {
                ICodedValueDomain CodeDomain = domain as ICodedValueDomain;
                for (int i = 0; i < CodeDomain.CodeCount; i++)
                    {
                        DataRow newRow = tblResult.NewRow();
                        newRow[0] = CodeDomain.get_Value(i).ToString();
                        newRow[1] = CodeDomain.get_Name(i);
                        tblResult.Rows.Add(newRow);
                    }
            }
        DataRow FistRow = tblResult.NewRow();
        //FistRow[1] = "Không chọn";
        tblResult.Rows.InsertAt(FistRow, 0);
        return tblResult;
        }

    public static DataTable ConvertDomainToTablefromWS(IWorkspace pWS, string pNameDomain)
        {

        IWorkspaceDomains pWSDomains;
        IDomain pDomain;
        pWSDomains = (IWorkspaceDomains) pWS;
        pDomain = (IDomain) pWSDomains.get_DomainByName(pNameDomain);
        DataTable tblResult = new DataTable();
        DataColumn colCode = new DataColumn("Code", typeof (string));
        DataColumn colValue = new DataColumn("Value", typeof (string));
        tblResult.Columns.Add(colCode);
        tblResult.Columns.Add(colValue);

        if (pDomain.Type == esriDomainType.esriDTCodedValue)
            {
                ICodedValueDomain CodeDomain = pDomain as ICodedValueDomain;
                for (int i = 0; i < CodeDomain.CodeCount; i++)
                    {
                        DataRow newRow = tblResult.NewRow();
                        newRow[0] = CodeDomain.get_Value(i).ToString();
                        newRow[1] = CodeDomain.get_Name(i);
                        tblResult.Rows.Add(newRow);
                    }
            }
        DataRow FistRow = tblResult.NewRow();
        //FistRow[1] = "Không chọn";
        tblResult.Rows.InsertAt(FistRow, 0);
        return tblResult;
        }

    /// <summary>
    /// Lấy tất cả Name của một domain
    /// </summary>
    /// <param name="domain"></param>
    /// <returns></returns>
    public static IList<string> GetAllNameValue(IDomain domain)
        {
        IList<string> lstNameValue = new List<string>();
        if (domain.Type == esriDomainType.esriDTCodedValue)
            {
                ICodedValueDomain CodeDomain = domain as ICodedValueDomain;
                for (int i = 0; i < CodeDomain.CodeCount; i++)
                    {
                        lstNameValue.Add(CodeDomain.get_Name(i));
                    }
            }
        return lstNameValue;
        }

    public static DataTable executeSQLQueryReturnDataTable(SqlCommand command)
        {
        DataTable dt;
        SqlDataReader dtReader;
        SqlConnection connect;
        connectionDB cn = new connectionDB();
        connect = cn.KetnoiADONet(generalVariable.server, generalVariable.database, generalVariable.username,
                                  generalVariable.password);
        connect.Open();

        dt = new DataTable();
        try
            {
                command.Connection = connect;
                dtReader = command.ExecuteReader();

                if (dtReader != null)
                    {
                        dt.Load(dtReader);
                    }
                else
                    {
                        dt = null;
                    }
            }
        catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                dt = null;
                connect.Close();
                //generalVariable.writeLogFileError("general_Model", "executeSQLQueryReturnDataTable", ex.Message);
            }
        connect.Close();
        return dt;
        }

    public static bool AddDomainToWS(IWorkspace pWS, string pNameDomain, object code, string value)
        {
        IWorkspaceDomains pWSDomains = (IWorkspaceDomains) pWS;
        ;
        IDomain pDomain = pWSDomains.get_DomainByName(pNameDomain);
        if (pDomain.Type == esriDomainType.esriDTCodedValue)
            {
                ICodedValueDomain CodeDomain = (ICodedValueDomain)pDomain;
                if (CodeDomain != null) CodeDomain.AddCode(code, value);
                IWorkspaceDomains2 ds =pWSDomains as IWorkspaceDomains2;
                ds.AlterDomain(pDomain);
                return true;
            }
        return false;
        }
    }
}

